package org.chickymate.client.view.panel;

import org.chickymate.client.HtmlConstants;
import org.chickymate.client.model.ModelDTO;

import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

public abstract class AbstractModelView extends Composite {
	
	private FlowPanel panel;

	protected AbstractModelView(ModelDTO model) {
		panel = new FlowPanel();
		panel.addStyleName(HtmlConstants.COMMENT_CONTAINER_STYLE_NAME);
		
		panel.add(getTitle(model));
		panel.add(getContent(model));
		initWidget(panel);
	}
	
	private Widget getTitle(ModelDTO model) {
		Panel title = createTitlePanel();
		title.add(getDateLabel(getDateContent(model)));
		title.add(createPositiveCommentLabel(model.getPositiveVotes()));
		title.add(new Label(" / "));
		title.add(createNegativeCommentLabel(model.getNegativeVotes()));
		return title;
	}
	
	protected abstract String getDateContent(ModelDTO model);
	
	protected abstract Widget getContent(ModelDTO model);
	
	private Label createPositiveCommentLabel(int votes) {
		Label label = createLabelWithStyle(" +" + votes, HtmlConstants.COMMENT_POSITIVE_STYLE_NAME);
		//TODO be able to vote
		return label;
	}

	private Label createNegativeCommentLabel(int votes) {
		Label label = createLabelWithStyle(" -" + votes, HtmlConstants.COMMENT_NEGATIVE_STYLE_NAME);
		//TODO be able to vote
		return label;
	}
	
	private Label getDateLabel(String date){
		return createLabelWithStyle(date, HtmlConstants.COMMENT_DATE_STYLE_NAME);
	}
	
	protected Panel createTitlePanel() {
		HorizontalPanel title = new HorizontalPanel();
		title.setStyleName(HtmlConstants.COMMENT_TITLE_STYLE_NAME);
		return title;
	}
	
	private Label createLabelWithStyle(String value, String style) {
		Label label = new Label(value);
		label.setStyleName(style);
		return label;
	}
	
	protected Anchor createUrlAnchor(String url) {
		Anchor link = new Anchor(url);
		link.setHref(url);
		link.setTarget("new");
		link.setStyleName(HtmlConstants.COMMENT_URL_STYLE_NAME);
		return link;
	}

	protected Anchor createUrlAnchorWithHttp(String url, String protocol) {
		Anchor link = createUrlAnchor(url);
		link.setHref(protocol + url);
		return link;
	}
}
